﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Read.API.Application.Commands.GodProductPlan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace SmartProduction.Read.API.Application.CommandHandlers.GodProductPlan
{
    /// <summary>
    /// 著生产计划查询命令处理
    /// </summary>
    public class ProductPlanQueryCommandHandler : IRequestHandler<ProductPlanQueryCommand, APIResult<APIPaing<ProductPlan>>>
    {
        /// <summary>
        /// 主生产计划仓储
        /// </summary>
        private readonly IBaseRepository<ProductPlan> product;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="product">主生产计划仓储</param>
        public ProductPlanQueryCommandHandler(IBaseRepository<ProductPlan> product, IMapper mapper)
        {
            this.product = product;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<APIPaing<ProductPlan>>> Handle(ProductPlanQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaing<ProductPlan>> result = new APIResult<APIPaing<ProductPlan>>();

            try
            {
                // 使用异步方法获取数据
                var allItems = await product.GetAll();

                var list = allItems.Where(x => x.IsDeleted == false);

                if (!string.IsNullOrEmpty(request.productcode))
                {
                    list = list.Where(x => x.PlanCode == request.productcode);
                }
                if (!string.IsNullOrEmpty(request.productname))
                {
                    list = list.Where(x => x.PlanName == request.productname);
                }
                if (request.isstatus!=null)
                {
                    list = list.Where(x => x.IsStatus == request.isstatus);
                }

                int totalcount = list.Count();
                int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.size);

                var pagelist = list.OrderByDescending(x => x.ProductPlanId).Skip((request.index - 1) * request.size).Take(request.size);

                result.Code = APIEnums.查询成功;
                result.Msg = APIEnums.查询成功.ToString();

                APIPaing<ProductPlan> page = new APIPaing<ProductPlan>();
                page.TotalCount = totalcount;
                page.PageCount = totalpage;
                page.Items = pagelist.ToList();

                result.data = page;
            }
            catch (Exception ex)
            {
                result.Code = APIEnums.查询失败; // 假设存在查询失败的枚举
                result.Msg = $"查询失败: {ex.Message}";
            }

            return result;
        }
    }
}